<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>借阅管理 - 图书管理系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
</head>
<body>
<nav th:replace="fragments/navbar :: navbar"></nav>

<div class="container mt-4">
    <div class="row mb-4">
        <div class="col-md-6">
            <h2>借阅管理</h2>
        </div>
        <div class="col-md-6 text-right">
            <a href="/borrows/new" class="btn btn-primary">新增借阅</a>
            <a href="/borrows/unreturned" class="btn btn-warning ml-2">未归还记录</a>
        </div>
    </div>

    <!-- 消息提示 -->
    <div th:if="${message}" class="alert alert-success alert-dismissible fade show" role="alert">
        <span th:text="${message}"></span>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>

    <div th:if="${error}" class="alert alert-danger alert-dismissible fade show" role="alert">
        <span th:text="${error}"></span>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>

    <!-- 借阅记录列表 -->
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead class="thead-dark">
                    <tr>
                        <th>ID</th>
                        <th>图书名称</th>
                        <th>读者姓名</th>
                        <th>借书时间</th>
                        <th>还书时间</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="record : ${records}">
                        <td th:text="${record.id}"></td>
                        <td th:text="${record.book.title}"></td>
                        <td th:text="${record.reader.name}"></td>
                        <td th:text="${#temporals.format(record.borrowDate, 'yyyy-MM-dd HH:mm:ss')}"></td>
                        <td th:text="${record.returnDate != null ? #temporals.format(record.returnDate, 'yyyy-MM-dd HH:mm:ss') : '未归还'}"></td>
                        <td>
                            <span class="badge"
                                  th:classappend="${record.returnDate == null ? 'badge-warning' : 'badge-success'}"
                                  th:text="${record.returnDate == null ? '借阅中' : '已归还'}"></span>
                        </td>
                        <td>
                            <a th:if="${record.returnDate == null}"
                               th:href="@{/borrows/return/{id}(id=${record.id})}"
                               class="btn btn-sm btn-success"
                               onclick="return confirm('确定要标记为已归还吗？')">还书</a>
                            <span th:if="${record.returnDate != null}">-</span>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
